import z from 'zod'
import { request } from '..'

// 获取题组列表
const getReadingQuestionGroupListRequestSchema = z.object({
  grade: z.union([z.null(), z.string()]).optional(),
  inClass: z.union([z.number(), z.null()]).optional(),
  pageNum: z.union([z.number(), z.null()]).optional(),
  pageSize: z.union([z.number(), z.null()]).optional(),
  readingType: z.union([z.null(), z.string()]).optional(),
  search: z.union([z.null(), z.string()]).optional(),
  textbookType: z.union([z.null(), z.string()]).optional(),
})
const readingQuestionSchema = z.object({
  readingId: z.string(),
  type: z.string(),
  extracurricularType: z.boolean().optional(),
  textType: z.union([z.null(), z.string()]).optional(),
  name: z.string(),
  grade: z.string().optional(),
  difficulty: z.number(),
  count: z.number(),
  isForbidden: z.boolean(),
})
const readingQuestionListSchema = z.array(readingQuestionSchema)
// 导出题组条目
export type ReadingQuestion = z.infer<typeof readingQuestionSchema>
export function getReadingQuestionGroupList(data: z.infer<typeof getReadingQuestionGroupListRequestSchema>) {
  return request({
    method: 'POST',
    url: '/reading/list',
    data,
    dataValidator: getReadingQuestionGroupListRequestSchema,
    responseValidator: z.object({
      entry: z.object({
        total: z.number(),
        readingList: z.object({
          ins: readingQuestionListSchema,
          outs: readingQuestionListSchema,
        }),
      }),
    }),
  })
}

// 获取单个题组的详情信息(包含多个小题)
const getReadingQuestionGroupDetailRequestSchema = z.object({
  readingId: z.union([z.null(), z.string()]).optional(),
})
const readingQuestionDetailSchema = z.object({
  analysis: z.string(),
  correctAnswer: z.string(),
  description: z.string(),
  questionId: z.string(),
  options: z.union([z.array(z.object({
    optionLabel: z.string(),
    optionText: z.string(),
  })), z.null()]).optional(),
  title: z.string(),
  type: z.literal(0).or(z.literal(1)),
})
export type ReadingQuestionDetail = z.infer<typeof readingQuestionDetailSchema>
export const readingQuestionGroupDetailSchema = z.object({
  author: z.string(),
  correspondingGrade: z.string(),
  count: z.number(),
  createTeacher: z.string(),
  description: z.string(),
  difficulty: z.number(),
  estimatedTime: z.number(),
  extracurricularType: z.union([z.null(), z.string()]).optional(),
  inClass: z.number(),
  isForbidden: z.boolean(),
  questions: z.array(readingQuestionDetailSchema),
  textbookType: z.union([z.null(), z.string()]).optional(),
  textType: z.union([z.null(), z.string()]).optional(),
  title: z.string(),
})
export type ReadingQuestionGroupDetail = z.infer<typeof readingQuestionGroupDetailSchema>
export function getReadingQuestionGroupDetail(params: z.infer<typeof getReadingQuestionGroupDetailRequestSchema>) {
  return request({
    method: 'GET',
    url: '/reading/detail',
    params,
    paramsValidator: getReadingQuestionGroupDetailRequestSchema,
    responseValidator: z.object({
      entry: readingQuestionGroupDetailSchema,
    }),
  })
}

// 新建/编辑一个题组（包含多个小题）  传id是编辑，不传是新建
// 先暂定题目类型为选择题，简答题，选择题为0，简答题为1
const createOrUpdateReadingQuestionGroupRequestSchema = z.object({
  author: z.string(),
  correspondingGrade: z.string(),
  createdBy: z.union([z.null(), z.string()]).optional(),
  description: z.string(),
  difficulty: z.number(),
  estimatedTime: z.number(),
  extracurricularType: z.union([z.null(), z.string()]).optional(),
  inClass: z.number(),
  questions: z.array(z.object({
    analysis: z.string(),
    correctAnswer: z.string(),
    options: z.union([z.array(z.object({
      label: z.string(),
      text: z.string(),
    })), z.null()]).optional(),
    orderIndex: z.number(),
    questionDescription: z.string(),
    questionTitle: z.string(),
    type: z.number(),
  })),
  readingId: z.union([z.null(), z.string()]).optional(),
  textbookType: z.union([z.null(), z.string()]).optional(),
  textType: z.union([z.null(), z.string()]).optional(),
  title: z.string(),
})
export function createOrUpdateReadingQuestionGroup(data: z.infer<typeof createOrUpdateReadingQuestionGroupRequestSchema>) {
  return request({
    method: 'POST',
    url: '/reading',
    data,
    dataValidator: createOrUpdateReadingQuestionGroupRequestSchema,
  })
}

// 修改题组状态
const toggleReadingQuestionGroupStatusRequestSchema = z.object({
  isForbidden: z.boolean(),
  readingId: z.string(),
})
export function toggleReadingQuestionGroupStatus(data: z.infer<typeof toggleReadingQuestionGroupStatusRequestSchema>) {
  return request({
    method: 'POST',
    url: '/reading/delete',
    data,
    dataValidator: toggleReadingQuestionGroupStatusRequestSchema,
  })
}

// 扫描图片中的文字
export function scanImageText(image: File) {
  const data = new FormData()
  data.append('file', image)
  return request({
    method: 'POST',
    url: 'reading/text/scan',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    responseValidator: z.object({
      entry: z.object({
        text: z.string(),
      }),
    }),
  })
}
